package com.cn.xiaonuo.slbfinances.slbagents.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slbagents.entity.SlXsProxyData;
import com.cn.xiaonuo.slbfinances.slbagents.enums.SlXsProxyDataExceptionEnum;
import com.cn.xiaonuo.slbfinances.slbagents.mapper.SlXsProxyDataMapper;
import com.cn.xiaonuo.slbfinances.slbagents.param.SlXsProxyDataParam;
import com.cn.xiaonuo.slbfinances.slbagents.service.SlbOperationsService;
import com.cn.xiaonuo.slbfinances.slbagents.service.XsProxyDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XsProxyDataServiceImpl extends ServiceImpl<SlXsProxyDataMapper, SlXsProxyData> implements XsProxyDataService {
    @Autowired
    private SlXsProxyDataMapper slXsProxyDataMapper;
    @Override
    @DataSource(name = "hpc_agent_db")
    public PageResult<SlXsProxyData> page(SlXsProxyDataParam agreementParam) {
        QueryWrapper<SlXsProxyData> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(agreementParam)) {

            // 根据唯一标识符 查询
            if (ObjectUtil.isNotEmpty(agreementParam.getId())) {
                queryWrapper.lambda().eq(SlXsProxyData::getId, agreementParam.getId());
            }

        }
        queryWrapper.lambda().orderByDesc(SlXsProxyData::getId);
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    @DataSource(name = "hpc_agent_db")
    public List<SlXsProxyData> list(SlXsProxyDataParam agreementParam) {
        LambdaQueryWrapper<SlXsProxyData> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(agreementParam)) {

            // 根据唯一标识符 查询
            if (ObjectUtil.isNotEmpty(agreementParam.getId())) {
                queryWrapper.eq(SlXsProxyData::getId, agreementParam.getId());
            }

        }
        return this.list(queryWrapper);
    }

    @Override
    @DataSource(name = "hpc_agent_db")
    public void add(SlXsProxyData slXsProxyData) {
        this.save(slXsProxyData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "hpc_agent_db")
    public void delete(SlXsProxyDataParam agreementParam) {
        this.removeById(agreementParam.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "hpc_agent_db")
    public void edit(SlXsProxyData slXsProxyData) {
        SlXsProxyData agreement = this.getById(slXsProxyData.getId());
        if (ObjectUtil.isNull(agreement)) {
            throw new ServiceException(SlXsProxyDataExceptionEnum.NOT_EXIST);
        }
        this.updateById(agreement);
    }

    @Override
    @DataSource(name = "hpc_agent_db")
    public SlXsProxyData detail(SlXsProxyDataParam agreementParam) {
        return this.querySlXsProxyData(agreementParam);
    }

    /**
     * 获取平台相关协议
     *
     * @author luyangyang
     * @date 2021-11-03 17:43:18
     */
    @DataSource(name = "hpc_agent_db")
    private SlXsProxyData querySlXsProxyData(SlXsProxyDataParam agreementParam) {
        SlXsProxyData agreement = this.getById(agreementParam.getId());
        if (ObjectUtil.isNull(agreement)) {
            throw new ServiceException(SlXsProxyDataExceptionEnum.NOT_EXIST);
        }
        return agreement;
    }
    /**
     * 获取平台相关协议
     *
     * @author luyangyang
     * @date 2021-11-03 17:43:18
     */
    @DataSource(name = "hpc_agent_db")
    public SlXsProxyData queryOne(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new SlXsProxyData() {{
            setUserId(Integer.valueOf(userId));
        }});
        //往sl_xs_proxy_data中加数据
        SlXsProxyData slXsProxyData = slXsProxyDataMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNull(slXsProxyData)) {
            throw new ServiceException(SlXsProxyDataExceptionEnum.NOT_EXIST);
        }
        return slXsProxyData;
    }



}
